#include "view/FormCarregaPerfil.h"
#include "controller/Controlador.h"
#include "model/Jogador.h"

using namespace Osp::App;
using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
using namespace Osp::Media;
using namespace Osp::Graphics;

FormCarregaPerfil::FormCarregaPerfil(void) {
}

FormCarregaPerfil::~FormCarregaPerfil(void) {
}

void FormCarregaPerfil::RetrievePlayers() {
	DatabaseManager* db = new DatabaseManager();
	db->Construct();

	perfis = db->GetPerfisOrderByCofre();

	if (__pList == null)
		return;

	InfoDB* jog;
	for (int i=0; i < perfis->GetCount(); i++) {
		jog = (InfoDB*)perfis->GetAt(i);
		String itemText = jog->GetNome();
		for (int j = 0; j < 30 - itemText.GetLength(); j++) {
			itemText.Append(" ");
		}
		itemText.Append(jog->GetCofre());
		__pList->AddItem(&itemText, null, null, null, ID_BASE_LISTA + i);
	}
}

bool FormCarregaPerfil::Initialize() {
	Construct(L"IDF_FORM_CARREGA_PERFIL");

	return true;
}

result FormCarregaPerfil::OnInitializing(void) {
	result r = E_SUCCESS;

	__pButtonVoltar = static_cast<Button *> (GetControl(L"IDC_BUTTON_VOLTAR"));
	if (__pButtonVoltar != null)
	{
		__pButtonVoltar->SetActionId(ID_BUTTON_VOLTAR);
		__pButtonVoltar->AddActionEventListener(*this);
	}

	__pButtonLimpar = static_cast<Button *>(GetControl(L"IDC_BUTTON_LIMPAR"));
	if (__pButtonLimpar != null)
	{
		__pButtonLimpar->SetActionId(ID_BUTTON_LIMPAR);
		__pButtonLimpar->AddActionEventListener(*this);
	}

	// Create a ContextMenu
	__pContextMenu = new ContextMenu();
	__pContextMenu->Construct(Point(0, 0), CONTEXT_MENU_STYLE_LIST);

	__pContextMenu->AddItem("Load", ID_CONTEXTMENU_LOAD);
	__pContextMenu->AddItem("Delete", ID_CONTEXTMENU_DELETE);
	__pContextMenu->AddActionEventListener(*this);

	// Create SlidableList
	__pList = static_cast<List *>(GetControl(L"IDC_LIST_PERFIS"));
	__pList->AddItemEventListener(*this);

	RetrievePlayers();

	return r;
}

result FormCarregaPerfil::OnTerminating(void) {
	result r = E_SUCCESS;

	delete __pContextMenu;

	return r;
}

void FormCarregaPerfil::OnActionPerformed(const Osp::Ui::Control& source,
		int actionId) {
	Frame *pFrame = Application::GetInstance()->GetAppFrame()->GetFrame();
	switch (actionId) {
	case ID_BUTTON_VOLTAR: {
		AppLog("VOLTAR Button is clicked! \n");
		FormMgr *pFormMgr = static_cast<FormMgr *> (pFrame->GetControl(
				"FormMgr"));
		if (pFormMgr != null)
			pFormMgr->SendUserEvent(FormMgr::REQUEST_FORM_MENU, null);
	}
		break;
	case ID_CONTEXTMENU_LOAD: {
		CarregarPerfil();

		FormMgr *pFormMgr = static_cast<FormMgr *> (pFrame->GetControl(
				"FormMgr"));
		if (pFormMgr != null)
			pFormMgr->SendUserEvent(FormMgr::REQUEST_FORM_JOGO, null);
	}
		break;
	case ID_CONTEXTMENU_DELETE:{
		DatabaseManager* db = new DatabaseManager();
		db->Construct();
		db->Remove(perfilSelecionado->GetNome());
		__pList->RemoveItemAt(indexSelecionado);
		__pList->RequestRedraw(true);

		delete db;
	}
		break;
	case ID_BUTTON_LIMPAR: {
		AppLog("Limpar Button is clicked! \n");
		DatabaseManager* db = new DatabaseManager();
		db->Construct();
		db->Limpar();
		__pList->RemoveAllItems();
		__pList->RequestRedraw(true);
		delete db;
	}
		break;

	default:
		break;
	}
}

result FormCarregaPerfil::OnDraw(void) {
	Image *pImage = new Image();

	result r = pImage->Construct();

	if (IsFailed(r))
		return r;
	Bitmap *pBitmap;

	pBitmap = pImage->DecodeN("/Home/background.png",
			BITMAP_PIXEL_FORMAT_ARGB8888);

	Canvas* pCanvas = GetCanvasN();
	if (pCanvas != null) {
		pCanvas->Clear();
		pCanvas->DrawBitmap(Point(0, 0), *pBitmap);
		pCanvas->Show();

		delete pCanvas;
	}

	delete pImage;
	delete pBitmap;

	return r;
}

void FormCarregaPerfil::CarregarPerfil() {
	String nome;
	int cofre;

	nome = perfilSelecionado->GetNome();
	cofre = perfilSelecionado->GetCofre();

	Controlador* controlador = Controlador::GetInstance();
	controlador->Construct();

	Jogador* jogador = new Jogador();
	jogador->Construct(nome, cofre);
	controlador->SetJogador(jogador);
}

void FormCarregaPerfil::OnItemStateChanged(const Osp::Ui::Control & source, int index, int itemId, Osp::Ui::ItemStatus status)
{
	//	InfoDB* perfil;
	perfilSelecionado = (InfoDB*)perfis->GetAt(index);
	indexSelecionado = index;

	// mostrar ContextMenu
//	__pContextMenu->SetPosition(__pList->GetPosition());
	int ultimo;
	ultimo = __pList->GetBottomDrawnItemIndex();
	__pContextMenu->SetPosition(128, (ultimo - (ultimo - index))*40 + 20);
	__pContextMenu->SetShowState(true);
	__pContextMenu->Show();
}

